import { createApp, App as VueInstance } from "vue";
import "./style.css";
import App from "./App.vue";
import {
	renderWithQiankun,
	qiankunWindow,
} from "vite-plugin-qiankun/dist/helper";


let app: VueInstance | null = null;

function render(props: any) {
	const { container } = props;
	app = createApp(App);
	app.mount(
		container
			? container.querySelector("#child-app")
			: document.getElementById("child-app")
	);
}
// some code
renderWithQiankun({
  bootstrap() {
		console.log("bootstrap");
	},
	mount(props) {
		console.log("mount");
		render(props);
	},
	unmount(props) {
		console.log("vue unmount");
		app!.unmount();
		(app!._container as HTMLDivElement).innerHTML = "";
		app = null;
	},
  update(props) {
    console.log(props)
  }
});

if (!qiankunWindow.__POWERED_BY_QIANKUN__) {
	render({});
}

// // 独立运行的时候
// if(!window.__POWERED_BY_QIANKUN__){
//   render()
// }

// export async function bootstrap() {
//   console.log('vue2 子应用创建')
// }

// export async function mount(props: any){
//   console.log('vue2 子应用加载')
//   render(props)
// }

// export async function unmount(){
//   console.log('vue2 子应用销毁')
//   instance.$destroy()
//   instance.$el.innerHtml = ''
//   instance = null
// }
